<template>
    <div class="container clearfix">
        <div class="main">
            <h1>用户注册</h1>
            <div class="input-box">
                <span>用户名</span>
                <el-input v-model="username" class="w-50 m-2" size="large" placeholder="请输入用户名" :prefix-icon="Avatar" />
            </div>
            <div class="input-box">
                <span>密码</span>
                <el-input v-model="password" class="w-50 m-2" size="large" placeholder="请输入密码" :prefix-icon="View"
                    show-password />
            </div>
            <div class="input-box">
                <span>确认密码</span>
                <el-input v-model="passwordAgain" class="w-50 m-2" size="large" placeholder="请再次输入密码" :prefix-icon="View"
                    show-password />
            </div>
            <div class="btn-box">
                <el-button type="primary" @click="register">注册</el-button>
                <el-button @click="$router.push('/login')">返回登录</el-button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { Avatar, View } from '@element-plus/icons-vue'
import { registerHttp } from '../../../request/api'

let $router = useRouter()
let username = ref('')
let password = ref('')
let passwordAgain = ref('')

//注册按钮
const register = () => {
    //检查用户名
    if (username.value.trim().replaceAll(' ', '') === "") {
        ElMessage({
            message: '用户名为空',
            type: 'warning',
        })
        return
    }

    //检查密码
    if (password.value !== passwordAgain.value) {
        ElMessage({
            message: '密码不一致',
            type: 'warning',
        })
        return
    }

    //注册接口
    registerHttp({
        username: username.value.trim().replaceAll(' ', ''),
        password: password.value,
        type: 'common'
    }).then(res => {
        if (res.data.code !== 200) {
            ElMessage({
                message: res.data.msg,
                type: 'warning',
            })
            return
        }
        ElMessage({
            message: res.data.msg,
            type: 'success',
        })
        $router.push("/")
    })
}


</script>

<style lang="scss" scoped>
.container {
    width: 100vw;
    height: 100vh;

    .main {
        width: 450px;
        margin: 200px auto;
        display: flex;
        flex-direction: column;
        gap: 14px;
        background: #ffffff;
        padding: 20px;
        border-radius: 5px;

        h1 {
            font-size: 25px;
            font-weight: 600;
            text-align: center;
            color: #4d4c4c;
        }

        .input-box {
            display: flex;
            height: 40px;

            span {
                padding-right: 10px;
                width: 20%;
                text-align-last: justify;
                line-height: 40px;
            }

            .el-input {
                width: 80%;
            }
        }

        .btn-box {
            width: 100%;
            display: flex;
            margin-top: 10px;

            .el-button {
                width: 50%;
                height: 40px;
                font-size: 16px;
            }
        }
    }
}
</style>